<?php
/**
 * 品牌管理控制器
 */
class BrandController extends AuthController{
    private $model;
    /**
     * 自动运行
     */
    public function __init(){
        parent::__init();
        $this->model = K('brand');
    } 
    /**
     * 品牌列表
     */
    public function index(){
        $this->assign('b_data',$this->model->order('sort')->all());
        $this->display();
    } 
    /**
     *  添加品牌
     */
    public function add_brand(){
    	if (IS_POST) {
            if(!$this->model->add_b_name()) $this->error($this->model->error);
            $this->success('添加品牌成功',U('index'),0.5);
        }
    	$this->display();
    }
    /**
     * uploadify上传的php处理方法
     */
    public function upload(){
        $upload = new Upload('Upload/Logo/' . date('y_m'));
        $file = $upload->upload();
        if (empty($file)) {
                //相当于 echo json_encode('上传失败');exit;
            $this->ajax('上传失败');
        } else {
               
            $data = $file[0];
            $this->ajax($data);
        }
    }

    /**
     *  编辑品牌
     */
    public function edit_brand(){
        // p(Q('get.bid'));
        if (IS_POST) { 
            if(!$this->model->edit_b_name()) $this->error($this->model->error);
            $this->success('编辑品牌成功',U('index'),0.5);
        }
        $bid = Q('get.bid',0,'intval');
        // 获取旧数据
        $old_b_data = $this->model->where("bid='{$bid}'")->find();
        $this->assign('old_b_data',$old_b_data);
        $this->display();
    }
    /**
     *  排序
     */
    public function sort(){ 
        if (IS_POST) {
            $s_data = Q('post.');
            // p($s_data);die;
            foreach ($s_data as $k => $v) {
                $this->model->where("bid='{$k}'")->update(array('sort'=>$v));
            } 
        }
        
        $this->success('排序成功',U('index'),0.5);
    } 

    /**
     *  删除品牌
     */
    public function del_brand(){
        $bid = Q('get.bid',0,'intval');
        $where = "bid='{$bid}'";
        // 删除品牌图片
        unlink($this->model->where($where)->getField('logo'));
        // 删除品牌
        $this->model->where($where)->delete();
        $this->success('删除成功',U('index'),0.5);
    }


 


}
